package com.dong.system.controller;


import com.dong.handler.BusinessException;
import com.dong.response.ResponseResult;
import com.dong.response.ResultCodeEnum;
import com.dong.system.entity.Department;
import com.dong.system.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author dongjian
 * @since 2021-01-23
 */

@RestController
@RequestMapping("/department")
@Api(value = "部门管理", tags = "部门管理接口")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "部门列表", notes = "查询部门信息和部门人数统计")
    @GetMapping("/findDeptAndCount")
    public ResponseResult findDeptAndCount() {
        List<Department> deptAndCounts = departmentService.findDeptAndCount();
        if (deptAndCounts.size() == 0) {
            throw new BusinessException(ResultCodeEnum.DEPARTMENT_NOT_FOUND_EXCEPTION.getCode(),
                    ResultCodeEnum.DEPARTMENT_NOT_FOUND_EXCEPTION.getMessage());
        }

        return ResponseResult.ok().data("deptAndCounts", deptAndCounts);
    }
}

